﻿
CREATE PROCEDURE [dbo].[CopiazaAnInchis]  
@pAn as int as
BEGIN

DECLARE @strAn varchar(4)
DECLARE @Comanda varchar(5000)

SET @strAn=CAST(@pAn AS varchar(4))

BEGIN TRAN 
PRINT('ArticoleAliniate')
--ArticoleAliniate---------------------------------------
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ArticoleAliniate'+@strAn+']') AND type in (N'U'))
BEGIN
	SET @Comanda = 'INSERT INTO [arhArticoleAliniate] SELECT a.*,'+@strAn+' FROM [ArticoleAliniate'+@strAn+'] a '
	EXEC(@Comanda) 
	IF @@ERROR != 0 GOTO EROARE
END
PRINT('Bilant')
--Bilant---------------------------------------
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Bilant'+@strAn+']') AND type in (N'U'))
BEGIN
	SET @Comanda = 'INSERT INTO [arhBilant] SELECT a.*,'+@strAn+' FROM [Bilant'+@strAn+'] a ' 
	EXEC(@Comanda) 
	IF @@ERROR != 0 GOTO EROARE
END

PRINT('Ordonantari')
--Bilant---------------------------------------
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Ordonantari'+@strAn+']') AND type in (N'U'))
BEGIN
	SET @Comanda = 'INSERT INTO [arhOrdonantari] SELECT a.*,'+@strAn+' FROM [Ordonantari'+@strAn+'] a ' 
	EXEC(@Comanda) 
	IF @@ERROR != 0 GOTO EROARE
END

PRINT('Conturi')
--Conturi---------------------------------------
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Conturi'+@strAn+']') AND type in (N'U'))
BEGIN
	SET @Comanda = 'INSERT INTO [arhConturi]([Cont],[CodUnitate],[Denumire],[Tip],[Terminal],[ContParinte],[EsteAnalitic],[DebitInitial],[CreditInitial],[DebitPrecedent],[CreditPrecedent],[Categorie],[RulajDebit],[RulajCredit],[Sold],[An]) SELECT [Cont],[CodUnitate],[Denumire],[Tip],[Terminal],[ContParinte],[EsteAnalitic],[DebitInitial],[CreditInitial],[DebitPrecedent],[CreditPrecedent],[Categorie],[RulajDebit],[RulajCredit],[Sold],'+@strAn+' FROM [Conturi'+@strAn+'] ' 
	EXEC(@Comanda) 
	IF @@ERROR != 0 GOTO EROARE
END

PRINT('Conturi Valuta')
--ConturiValuta---------------------------------------
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ConturiValuta'+@strAn+']') AND type in (N'U'))
BEGIN
	SET @Comanda = 'INSERT INTO [arhConturiValuta] SELECT a.*,'+@strAn+' FROM [ConturiValuta'+@strAn+'] a ' 
	EXEC(@Comanda) 
	IF @@ERROR != 0 GOTO EROARE
END

PRINT('JurnaleSolduriInitiale')
--JurnaleSolduriInitiale---------------------------------------
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[JurnaleSolduriInitiale'+@strAn+']') AND type in (N'U'))
BEGIN
	SET @Comanda = 'INSERT INTO [arhJurnaleSolduriInitiale] SELECT a.*,'+@strAn+' FROM [JurnaleSolduriInitiale'+@strAn+'] a'
	EXEC(@Comanda) 
	IF @@ERROR != 0 GOTO EROARE
END

PRINT('RezultatPatrimonial')
--RezultatPatrimonial---------------------------------------
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RezultatPatrimonial'+@strAn+']') AND type in (N'U'))
BEGIN
	SET @Comanda = 'INSERT INTO [arhRezultatPatrimonial] SELECT a.*,'+@strAn+' FROM [RezultatPatrimonial'+@strAn+'] a'
	EXEC(@Comanda) 
	IF @@ERROR != 0 GOTO EROARE
END


PRINT('RulajeDoc')
--RulajeDoc---------------------------------------
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RulajeDoc'+@strAn+']') AND type in (N'U'))
BEGIN
	SET @Comanda = 'INSERT INTO [arhRulajeDoc] SELECT a.*,'+@strAn+' FROM [RulajeDoc'+@strAn+'] a'
	EXEC(@Comanda) 
	IF @@ERROR != 0 GOTO EROARE
END

PRINT('Rulaje')
--Rulaje---------------------------------------
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rulaje'+@strAn+']') AND type in (N'U'))
BEGIN
	SET @Comanda = 'INSERT INTO [arhRulaje] SELECT a.*,'+@strAn+' FROM [Rulaje'+@strAn+'] a'
	EXEC(@Comanda) 
	IF @@ERROR != 0 GOTO EROARE
END

PRINT('VenituriSiCheltuieli')
--VenituriSiCheltuieli---------------------------------------
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[VenituriSiCheltuieli'+@strAn+']') AND type in (N'U'))
BEGIN
	SET @Comanda = 'INSERT INTO [arhVenituriSiCheltuieli] SELECT a.*,'+@strAn+' FROM [VenituriSiCheltuieli'+@strAn+'] a'
	EXEC(@Comanda) 
	IF @@ERROR != 0 GOTO EROARE	
END	


PRINT('Sterge Ordonantari')
SET @Comanda = 'DROP TABLE [Ordonantari'+@strAn+']'
EXEC(@Comanda) 
IF @@ERROR != 0 GOTO EROARE

PRINT('Sterge rulaje')
SET @Comanda = 'DROP TABLE [Rulaje'+@strAn+']'
EXEC(@Comanda) 
IF @@ERROR != 0 GOTO EROARE

PRINT('Sterge rulajeDoc')
SET @Comanda = 'DROP TABLE [RulajeDoc'+@strAn+']'
EXEC(@Comanda) 
IF @@ERROR != 0 GOTO EROARE

SET @Comanda = 'DROP TABLE [ConturiValuta'+@strAn+']' 
EXEC(@Comanda) 
IF @@ERROR != 0 GOTO EROARE

SET @Comanda = 'DROP Table [JurnaleSolduriInitiale'+@strAn+']'
EXEC(@Comanda) 
IF @@ERROR != 0 GOTO EROARE

--SET @Comanda = 'DELETE FROM [Ordonantari] WHERE YEAR(DataDoc)='+@strAn +' AND [CodUnitate]=' + @strCodUnitate
--EXEC(@Comanda) 
--IF @@ERROR != 0 GOTO EROARE

SET @Comanda = 'DROP TABLE [RezultatPatrimonial'+@strAn+']'
EXEC(@Comanda) 
IF @@ERROR != 0 GOTO EROARE


SET @Comanda = 'DROP TABLE [VenituriSiCheltuieli'+@strAn+']'
EXEC(@Comanda) 
IF @@ERROR != 0 GOTO EROARE

SET @Comanda = 'DROP TABLE [Bilant'+@strAn+'] ' 
EXEC(@Comanda) 
IF @@ERROR != 0 GOTO EROARE

SET @Comanda = 'DROP TABLE [Conturi'+@strAn+']' 
EXEC(@Comanda) 
IF @@ERROR != 0 GOTO EROARE

SET @Comanda = 'DROP TABLE [ArticoleAliniate'+@strAn+']' 
EXEC(@Comanda) 
IF @@ERROR != 0 GOTO EROARE


COMMIT

GOTO Gata
EROARE:
	ROLLBACK
	RAISERROR ('Eroare la inchiderea anului!',16,-1)
	GOTO Gata
EROARE1:
	RAISERROR ('!!',16,-1)	
	GOTO Gata
Gata:

END